package com.mxpio.erp.technology.policy.impl;

import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.*;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/11/22 22:34
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Component
public class ItemDeletePolicy extends SmartCrudPolicyAdapter {
    @Autowired
    private ItemLeadTimePropService itemLeadTimePropSerivce;
    @Autowired
    private ItemPlanPropService itemPlanPropSerivce;
    @Autowired
    private ItemPurchasePropService itemPurchasePropSerivce;
    @Autowired
    private ItemStorePropService itemStorePropSerivce;
/*    @Autowired
    private ItemMaterialPropService itemMaterialPropSerivce;*/
    @Override
    public boolean beforeDelete(CrudContext context) {
        Object o = context.getEntity();
        if(o instanceof Item){
            Item item = (Item) o;

            ItemPropKey itemPropKey = new ItemPropKey();
            itemPropKey.setPropType("item");
            itemPropKey.setCode(item.getItemCode());
            ItemLeadTimeProp itemLeadTimeProp = itemLeadTimePropSerivce.getById(ItemLeadTimeProp.class, itemPropKey);
            if (itemLeadTimeProp!=null){//直接用service的delete方法时，如果遇到子表数据不存在的情况，就会报错
                JpaUtil.delete(itemLeadTimeProp);
            }
				/*ItemMaterialProp itemMaterialProp = itemMaterialPropSerivce.getById(ItemMaterialProp.class, key);
				if (itemMaterialProp!=null){
					JpaUtil.delete(itemMaterialProp);
				}*/
            ItemPlanProp itemPlanProp = itemPlanPropSerivce.getById(ItemPlanProp.class, itemPropKey);
            if (itemPlanProp!=null){
                JpaUtil.delete(itemPlanProp);
            }
            ItemPurchaseProp itemPurchaseProp = itemPurchasePropSerivce.getById(ItemPurchaseProp.class, itemPropKey);
            if (itemPurchaseProp!=null){
                JpaUtil.delete(itemPurchaseProp);
            }
            ItemStoreProp itemStoreProp = itemStorePropSerivce.getById(ItemStoreProp.class, itemPropKey);
            if (itemStoreProp!=null){
                JpaUtil.delete(itemStoreProp);
            }
        }
        return true;
    }
}
